package zy.blue7.common.vaildate.custom.rules.impl;


import lombok.Getter;
import org.springframework.validation.Errors;
import zy.blue7.common.vaildate.ValidationAbstract;
import zy.blue7.common.vaildate.custom.rules.RuleInterface;
import zy.blue7.common.vo.req.SaveUserDto;

import java.util.Arrays;

/**
 * @author blue7
 * @version 1.0
 * @date 2021/11/22 11:19
 */
public class NameNotNullRuleInterfaceImpl<T extends SaveUserDto> implements RuleInterface {
    @Override
    public Integer getLevel() {
        return 3;
    }

    @Getter
    private String errorCode;

    public NameNotNullRuleInterfaceImpl(String errorCode) {
        this.errorCode = errorCode;
    }

    @Override
    public void validate(Object target, Errors errors, Object... validationHints) {
        doValidate((T) target, errors, validationHints);
    }

    private void doValidate(T target, Errors errors, Object[] validationHints) {

        System.out.println(target);
        System.out.println(Arrays.toString(validationHints));
        // 这里只是做个简单的demo
        if (1 == 0) {
            errors.reject(errorCode, "error");
        }
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return clazz.isInstance(ValidationAbstract.class);
    }

    @Override
    public void validate(Object target, Errors errors) {
        doValidate((T) target, errors, null);
    }
}
